import { _decorator, Component, Label, Node, Sprite, Tween, tween, Vec3 } from 'cc';
import { GunInfo } from '../../../Data/GlobaInterface';
import { GAME_POP, GlobalData } from '../../../Data/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('SingleEnemy')
/**
 * 单个敌人
 */
export class SingleEnemy extends Component {

    //移动速度
    private Vx: number = 0;
    private Vy: number = 0;

    private HorFixVx: number = 1;
    private VerFixVy: number = 1;

    private RemVx: number = 0;
    private AddVy: number = 0;

    //纵向移动距离
    private VerDistance: number = 130;

    //边界
    private BuiderX: number = 300;

    //
    @property(Sprite)
    private Enemy0: Sprite = null;

    //
    @property(Sprite)
    private Enemy1: Sprite = null;

    //
    @property(Sprite)
    private Enemy2: Sprite = null;

    //样式索引
    public ClearIndex: number = 0;

    //不移动
    public IsMove: boolean = false;

    //测试文字
    @property(Label)
    private TestText: Label = null;


    public NowIndex: number = 0;
    //当前移动索引
    public MoveIndex: number = 0;

    //我的方向
    public MyDir: number = 0;

    protected onLoad(): void {
        //测试
        this.UpdateStyle(0)
    }

    private testtime: number = 0;
    public update0(dt: number): void {
        if (GlobalData.GameOver) return;
        this.IamMove(this.IsMove ? 1 : -3);
    }

    /**
     * 向下移动
     */
    private DownMove() {
        this.Vx = 0;
        this.Vy = -this.VerFixVy;
        this.AddVy = 0;
    }

    /**
     * 设置速度
     */
    private SetMoveSpeed() {

    }

    /**
     * 更新样式
     */
    public UpdateStyle(_clearindex: number) {

        this.MoveIndex = 0;
        this.NowIndex = 0;

        this.IsMove = true;
        this.Vx = this.HorFixVx;
        this.AddVy = 0;
        this.Vy = 0;
        this.ClearIndex = _clearindex;
        if (_clearindex < 8) {

            this.Enemy1.node.active = true;
            this.Enemy2.node.active = false;

            this.Enemy0.spriteFrame = app.resManager.SpriteFrames["zumal_clear" + (this.ClearIndex + 1)]
            this.Enemy1.spriteFrame = app.resManager.SpriteFrames["zumal_clear" + (this.ClearIndex + 1)]
            this.Enemy2.spriteFrame = app.resManager.SpriteFrames["zumal_clear" + (this.ClearIndex + 1)]
        } else {

            this.Enemy1.node.active = false;
            this.Enemy2.node.active = false;

            this.Enemy0.spriteFrame = app.resManager.SpriteFrames["zumal_clear" + (9)]
        }

        this.MyDir = 0;
    }

    /**
     * 
     */
    public UpdateIndex(_index: number) {
        this.TestText.string = _index.toString();
    }

    //当前得角度
    private CurrentAngle: number = 0;
    private ChangeAngle: number = 0;

    /**
     * 移动
     */
    private IamMove(_move: number) {

        this.MoveIndex += _move;
        //判断我和下一个(先简单的横纵判断，后续写一个贝赛尔线工具)
        let move_index: number = 0;

        let vx: number = 0;
        let vy: number = 0;

        for (let i = 0; i < GlobalData.MoveSpace.length; i++) {//要计算的是我在哪个位置移动多少
            if (this.MoveIndex >= GlobalData.MoveSpace[i]) {
                move_index = i + 1;
            }
        }

        if (move_index > GlobalData.MoveSpace.length - 1) {//游戏结束
            app.popManager.ShowPop(GAME_POP.POPENDBOX)
            return;
        }

        let star_pos: Vec3 = new Vec3(GlobalData.MovePath[move_index].star.x, GlobalData.MovePath[move_index].star.y, 0);
        // 
        //判断方向
        if (GlobalData.MovePath[move_index].end.x > GlobalData.MovePath[move_index].star.x) {
            vx = GlobalData.MoveSpeed;
            this.CurrentAngle = 0;
        } else if (GlobalData.MovePath[move_index].end.x < GlobalData.MovePath[move_index].star.x) {
            vx = -GlobalData.MoveSpeed;
            this.CurrentAngle = -180;
        }

        if (GlobalData.MovePath[move_index].end.y > GlobalData.MovePath[move_index].star.y) {
            vy = GlobalData.MoveSpeed;
            this.CurrentAngle = 90;
        } else if (GlobalData.MovePath[move_index].end.y < GlobalData.MovePath[move_index].star.y) {
            vy = -GlobalData.MoveSpeed;
            this.CurrentAngle = -90;
        }


        let add_dis: number = this.MoveIndex - (move_index == 0 ? 0 : GlobalData.MoveSpace[move_index - 1]);

        this.node.setPosition(star_pos.add(new Vec3(vx * add_dis, vy * add_dis)));

        //角度问题
        if (this.ChangeAngle != this.CurrentAngle) {
            this.node.angle = this.ChangeAngle;
            this.ChangeAngle = this.CurrentAngle;
            Tween.stopAllByTarget(this.node);
            tween(this.node).to(0.5 / GlobalData.GameSpeed, { angle: this.ChangeAngle }).start();
        }

    }

    /**
     * 我被击中了
     */
    public IamFired(_guninfo: GunInfo): boolean {
        if (_guninfo.Style == this.ClearIndex) {//我就销毁了
            return true;
        }
        return false;
    }

}


